package process_demo.review.wait_notify;

import java.util.concurrent.TimeUnit;

/**
 * @author haomin
 * @date 2022/09/12 11:42
 **/
public class Demo1 {
    static class MyThread extends Thread{
        private Object o;
        public MyThread (Object o){
            this.o = o;
        }

        @Override
        public void run() {
            synchronized (o){
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("执行唤醒命令");
                o.notify();
            }
        }
    }


    public static void main(String[] args) throws InterruptedException{
        Object o = new Object();

        synchronized(o){
            MyThread t = new MyThread(o);
            t.start();

            o.wait(); // 1 释放o 锁，2 等待， 3 再次加锁
                      // wait 等待时不持有锁
            System.out.println("唤醒后才当执行到这");
        }
    }
}